package com.school.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.manager.common.Constants;
import com.school.manager.domaim.SysUser;
import com.school.manager.domaim.SysUserRole;
import com.school.manager.entity.ResponseEntity;
import com.school.manager.entity.po.SysUserPo;
import com.school.manager.entity.vo.SysUserVo;
import com.school.manager.enums.ExceptionEnum;
import com.school.manager.enums.ResponseEnum;
import com.school.manager.enums.StatusEnum;
import com.school.manager.exception.BussinessException;
import com.school.manager.service.ISysUserRoleService;
import com.school.manager.service.ISysUserService;
import com.school.manager.utils.ConvertUtil;
import com.school.manager.utils.MyContextHolder;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * description：系统用户控制器
 * date：2021/10/133:18 下午
 */

@RestController
@Api(tags = "系统用户")
@RequestMapping(Constants.PROGRAM_PATH + "sysuser")
public class SysUserController {
	
	@Autowired
    private ISysUserService sysUserService;
	
	@Autowired
	private ISysUserRoleService userRoleService;
	
	@Value("${system.super.admin}")
	private String superadmin;
	
	@Value("${user.init.password}")
	private String password;

	@PostMapping("add")
    @ApiOperation("新增系统用户")
    @ApiImplicitParam(name = "系统用户", value = "sysUserPo", dataTypeClass = SysUserPo.class)
    public ResponseEntity add(@RequestBody SysUserPo sysUserPo) {
		String password = sysUserPo.getPassword();
		// 密码加密
		sysUserPo.setPassword(DigestUtil.md5Hex(password));
		int count = sysUserService.count(null);
		sysUserPo.setUserOrder(++count);
		SysUser sysUser = ConvertUtil.convertObj(sysUserPo, SysUser.class);
		sysUserService.save(sysUser);
		// 设置权限代码
		SysUserRole userRole = new SysUserRole();
		userRole.setRoleId(sysUserPo.getRoleId());
		userRole.setUserId(sysUser.getId());
		userRoleService.save(userRole);
        return ResponseEnum.SUCCESS.success();
    }


    @PostMapping("edit")
    @ApiOperation("修改系统用户")
    @ApiImplicitParam(name = "系统用户", value = "sysUserPo", dataTypeClass = SysUserPo.class)
    public ResponseEntity edit(@RequestBody SysUserPo sysUserPo) {
        return ResponseEnum.SUCCESS.success(sysUserService.updateById(ConvertUtil.convertObj(sysUserPo, SysUser.class)));
    }


    @GetMapping("get/{id}")
    @ApiOperation("系统用户详情")
    @ApiImplicitParam(name = "系统用户主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity get(@PathVariable("id") String id) {
        SysUser sysUser = sysUserService.getById(id);
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertObj(sysUser, SysUserVo.class));
    }


    @PostMapping("page")
    @ApiOperation("系统用户分页列表")
    @ApiImplicitParam(name = "系统用户Po", value = "sysUserPo", dataTypeClass = SysUserPo.class)
    public ResponseEntity page(@RequestBody SysUserPo sysUserPo) {
    	// 用户查询全部
    	sysUserPo.setStatus(StrUtil.EMPTY);
        return ResponseEnum.SUCCESS.success(sysUserService.page(sysUserPo));
    }
    
    @PostMapping("pagebyclass")
    @ApiOperation("系统用户分页列表")
    @ApiImplicitParam(name = "系统用户Po", value = "sysUserPo", dataTypeClass = SysUserPo.class)
    public ResponseEntity pageByClass(@RequestBody SysUserPo sysUserPo) {
    	Page<SysUserVo> result = sysUserService.pageByClass(sysUserPo);
    	return ResponseEnum.SUCCESS.success(result);
    }
    
    @PostMapping("pagenoclass")
    @ApiOperation("系统用户分页列表")
    @ApiImplicitParam(name = "系统用户Po", value = "sysUserPo", dataTypeClass = SysUserPo.class)
    public ResponseEntity pageNoClass(@RequestBody SysUserPo sysUserPo) {
    	Page<SysUserVo> result = sysUserService.pageNoClass(sysUserPo);
    	return ResponseEnum.SUCCESS.success(result);
    }

    @GetMapping("del/{id}")
    @ApiOperation("禁用系统用户")
    @ApiImplicitParam(name = "id", value = "id", dataTypeClass = String.class)
    public ResponseEntity del(@PathVariable("id") String id) {
    	this.checkUserOperate(id);
    	SysUser user = sysUserService.getById(id);
    	user.setStatus(StatusEnum.DISABLE.getCode());
        return ResponseEnum.SUCCESS.success(sysUserService.updateById(user));
    }
    
    @GetMapping("enable/{id}")
    @ApiOperation("禁用系统用户")
    @ApiImplicitParam(name = "id", value = "id", dataTypeClass = String.class)
    public ResponseEntity enable(@PathVariable("id") String id) {
    	this.checkUserOperate(id);
    	SysUser user = sysUserService.getById(id);
    	user.setStatus(StatusEnum.ENABLE.getCode());
        return ResponseEnum.SUCCESS.success(sysUserService.updateById(user));
    }
    
    @GetMapping("reset/{id}")
    @ApiOperation("重置密码")
    @ApiImplicitParam(name = "id", value = "id", dataTypeClass = String.class)
    public ResponseEntity reset(@PathVariable("id") String id) {
    	this.checkUserOperate(id);
    	SysUser user = sysUserService.getById(id);
    	user.setPassword(DigestUtil.md5Hex(password));
        return ResponseEnum.SUCCESS.success(sysUserService.updateById(user));
    }
    
    /**
     * 用户操作校验
     * @param id
     */
    private void checkUserOperate(String id) {
    	if (MyContextHolder.getCurrentUser().getId().equals(id)) {
			throw new BussinessException(ExceptionEnum.CURRENT_USER_NO_OPERAT);
		}
    	if (superadmin.equals(id)) {
    		throw new BussinessException(ExceptionEnum.SUPER_ADMIN_NO_OPERAT);
		}
    }
}
